package lib.self.ex.decor;

import android.content.Context;
import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import lib.self.ConstEx.TitleBarState;
import lib.self.ConstEx.ViewState;
import lib.self.R;
import lib.self.ex.ParamsEx;
import lib.self.ex.interfaces.OnRetryClickListener;
import lib.self.util.view.LayoutUtil;
import lib.self.util.view.ViewUtil;

/**
 * description: 装饰的View
 * author: Steven Xiao
 * date: 2017/5/2
 */
public class DecorView extends RelativeLayout {
    private static final String TAG = DecorView.class.getSimpleName();
    private static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
    private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

    private TitleBarEx mTitleBar;
    private View mContentHeaderView;
    private View mContentView;
    private FailureView mFailureView;
    private LoadingView mLoadingView;

    private int mViewState = ViewState.KViewStateNormal;

    private int mTitleBarState;

    public DecorView(Context context, @IntRange(from = 0, to = 1) int state) {
        super(context);
        mTitleBarState = state;
        init(context);
    }

    private void init(Context context) {
        setLayoutParams(LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.MATCH_PARENT));
        /**
         * 背景色
         */
        if (ParamsEx.getAppBgDrawableId() != 0) {
            setBackgroundResource(ParamsEx.getAppBgDrawableId());
        } else {
            if (ParamsEx.getAppBgColor() != 0) {
                setBackgroundColor(ParamsEx.getAppBgColor());
            }
        }

        //添加titleBar
        mTitleBar = new TitleBarEx(context);
        LayoutParams params = LayoutUtil.getRelativeParams(MATCH_PARENT, WRAP_CONTENT);
        addView(mTitleBar, params);
        mTitleBar.gone();
    }

    public void setContentView(@LayoutRes int contentViewId, @LayoutRes int headerViewId) {
        LayoutParams params = LayoutUtil.getRelativeParams(LayoutUtil.MATCH_PARENT, LayoutUtil.MATCH_PARENT);
        boolean isTitleAbove = false;
        if (mTitleBarState == TitleBarState.KAbove) {
            isTitleAbove = true;
        }

        if (headerViewId != 0) {
            mContentHeaderView = inflate(getContext(), headerViewId, null);
            mContentHeaderView.setId(R.id.content_header_view);
            LayoutParams headerParams = LayoutUtil.getRelativeParams(MATCH_PARENT, WRAP_CONTENT);
            if (isTitleAbove) {
                addView(mContentHeaderView, 0, headerParams);
            } else {
                headerParams.addRule(BELOW, mTitleBar.getId());
                addView(mContentHeaderView, headerParams);
            }
            params.addRule(BELOW, mContentHeaderView.getId());
        } else {
            if (!isTitleAbove) {
                params.addRule(BELOW, mTitleBar.getId());
            }
        }

        mContentView = inflate(getContext(), contentViewId, null);
        if (isTitleAbove) {
            addView(mContentView, 0, params);
        } else {
            addView(mContentView, params);
        }

        mFailureView = new FailureView(getContext());
        if (isTitleAbove) {
            addView(mFailureView, 0, params);
        } else {
            addView(mFailureView, params);
        }
        goneView(mFailureView);

        mLoadingView = new LoadingView(getContext());
        if (isTitleAbove) {
            addView(mLoadingView, 0, params);
        } else {
            addView(mLoadingView, params);
        }
        goneView(mLoadingView);
    }

    public void setContentView(@LayoutRes int contentViewId) {
        setContentView(contentViewId, 0);
    }

    public View getContentView() {
        return mContentView;
    }

    public TitleBarEx getTitleBar() {
        return mTitleBar;
    }

    public View getContentHeaderView() {
        return mContentHeaderView;
    }

    public void setViewState(@IntRange(from = 0, to = 2) int state) {
        if (mViewState == state) {
            return;
        }
        mViewState = state;
        switch (mViewState) {
            case ViewState.KViewStateNormal:
                showView(mContentView);

                goneView(mFailureView);
                goneView(mLoadingView);
                break;
            case ViewState.KViewStateLoading:
                showView(mLoadingView);

                goneView(mContentView);
                goneView(mFailureView);
                break;
            case ViewState.KViewStateFailed:
                showView(mFailureView);

                goneView(mLoadingView);
                goneView(mContentView);
                break;
        }
    }

    public int getViewState() {
        return mViewState;
    }

    public void setOnRetryClickListener(OnRetryClickListener listener) {
        mFailureView.setOnRetryClickListener(listener);
    }

    private void showView(View v) {
        ViewUtil.showView(v);
    }

    private void goneView(View v) {
        ViewUtil.goneView(v);
    }

}
